#include "game.h"

Game::Game(){
   this->score = 0;
   this->randomFood();
   srand(time(NULL));
}

void Game::randomFood(){
   this->food_pos[0] = rand() % 21 + 1;
   this->food_pos[1] = rand() % 77 + 1;
}

void Game::gameRedraw(){
   refresh();
   clear();
   
   mvaddch(this->food_pos[0], this->food_pos[1], ACS_BULLET);
   
   if (this->player_number == 2){
      for(int l = 0; l < this->Snake1->getSnakeLengh(); l++){
         mvaddch(this->Snake1->getSnakePos(l, 0), this->Snake1->getSnakePos(l, 1), ACS_DIAMOND);
      }

      for(int l = 0; l < this->Snake2->getSnakeLengh(); l++){
         mvaddch(this->Snake2->getSnakePos(l, 0), this->Snake2->getSnakePos(l, 1), ACS_DIAMOND);
      }
   }
   else if (this->player_number == 1){
      for(int l = 0; l < this->Snake1->getSnakeLengh(); l++){
         mvaddch(this->Snake1->getSnakePos(l, 0), this->Snake1->getSnakePos(l, 1), ACS_DIAMOND);
      }
   }
}

void Game::gameThink(){
   if (this->player_number == 1){
      if(this->Snake1->getSnakePos(0, 0) == this->food_pos[0] && this->Snake1->getSnakePos(0, 1) == this->food_pos[1]){
         this->Snake1->snakeAppend();
         this->randomFood();
      }
      
      for (int x = 1; x < this->Snake1->getSnakeLengh(); x++){
         if (this->Snake1->getSnakePos(0, 0) == this->Snake1->getSnakePos(x, 0) && this->Snake1->getSnakePos(0, 1) == this->Snake1->getSnakePos(x, 1))
            this->Snake1->snakeDie();
      }
   }
   
   else if (this->player_number == 2){
      if(this->Snake1->getSnakePos(0, 0) == this->food_pos[0] && this->Snake1->getSnakePos(0, 1) == this->food_pos[1]){
         this->Snake1->snakeAppend();
         this->randomFood();
      }
      
      for (int x = 1; x < this->Snake1->getSnakeLengh(); x++){
         if (this->Snake1->getSnakePos(0, 0) == this->Snake1->getSnakePos(x, 0) && this->Snake1->getSnakePos(0, 1) == this->Snake1->getSnakePos(x, 1))
            this->Snake1->snakeDie();
      }
      
      if(this->Snake2->getSnakePos(0, 0) == this->food_pos[0] && this->Snake2->getSnakePos(0, 1) == this->food_pos[1]){
         this->Snake2->snakeAppend();
         this->randomFood();
      }
      
      for (int x = 1; x < this->Snake2->getSnakeLengh(); x++){
         if (this->Snake2->getSnakePos(0, 0) == this->Snake2->getSnakePos(x, 0) && this->Snake2->getSnakePos(0, 1) == this->Snake2->getSnakePos(x, 1))
            this->Snake2->snakeDie();
      }
   }   
   
}

void Game::gameStart(Snake* Snake1, Snake* Snake2){
   refresh();
   clear();
   
   this->Snake1 = Snake1;
   this->Snake2 = Snake2;
   
   int z;
   
   halfdelay(1);
   
   if (this->player_number == 1){
      while ((z = getch()) != 27){
         this->Snake1->snakeMove(z);
         this->Snake1->snakeRun();
         this->gameThink();
         this->gameRedraw();
      }
      this->exitGame();
   }
   else if (this->player_number == 2){
      while ((z = getch()) != 27){
         this->Snake1->snakeMove(z);
         this->Snake2->snakeMove(z);
         this->Snake1->snakeRun();
         this->Snake2->snakeRun();
         this->gameThink();
         this->gameRedraw();
      }
      this->exitGame();
   }
}

void Game::exitGame(){
   echo();
   endwin();
}
